package com.example.novelweb.controller;


import com.example.novelweb.common.BaseResponse;
import com.example.novelweb.common.ErrorCode;
import com.example.novelweb.exception.ThrowUtils;
import com.example.novelweb.model.dto.wordrule.WordRuleAddRequest;
import com.example.novelweb.model.dto.wordrule.WordRuleDelRequest;
import com.example.novelweb.model.entity.Wordrule;
import com.example.novelweb.service.WordruleService;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/wordrule")
public class WordRuleController {

  @Resource
  private WordruleService wordruleService;

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @GetMapping("/list")
  public BaseResponse<List<Wordrule>> listRules(HttpServletRequest httpServletRequest) {
    return wordruleService.listAll();
  }


  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/add")
  public BaseResponse<Boolean> addWords(@RequestBody WordRuleAddRequest wordRuleAddRequest, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(wordRuleAddRequest == null, ErrorCode.PARAMS_ERROR);
    ThrowUtils.throwIf(StringUtils.isBlank(wordRuleAddRequest.getWordList()),ErrorCode.PARAMS_ERROR);


    return wordruleService.addWords(wordRuleAddRequest);

  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/del")
  public BaseResponse<Boolean> delWords(@RequestBody WordRuleDelRequest wordRuleDelRequest, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(wordRuleDelRequest == null, ErrorCode.PARAMS_ERROR);
    ThrowUtils.throwIf(StringUtils.isBlank(wordRuleDelRequest.getWordList())
        ,ErrorCode.PARAMS_ERROR);

    return wordruleService.delWords(wordRuleDelRequest);
  }




}
